/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.packet;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import lineage.config.L1Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 服务端封包的抽象类.
 * 
 * @version 2014年3月10日下午4:45:21
 * @author jrwz
 */
public abstract class L1AbstractPacketServer {
    private static final Logger LOG = LoggerFactory.getLogger(L1AbstractPacketServer.class);
    /** 客户端编码. */
    private static final String CLIENT_LANGUAGE_CODE = L1Config.CLIENT_LANGUAGE_CODE;
    /** 带缓冲区的字节数组输出流. */
    protected ByteArrayOutputStream bao = new ByteArrayOutputStream();

    /**
     * 取得内容.
     * 
     * @return 内容
     * @throws IOException
     */
    public abstract byte[] getContent() throws IOException;

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param flag
     *            - true:0x01 false:0x00
     * @return null
     */
    protected Object writeBoolean(final boolean flag) {
        bao.write(flag ? 0x01 : 0x00);
        return null;
    }

    /**
     * 将指定'字节数组'写入该字节数组输出流.
     * 
     * @param text
     *            - 要写入的字节数组
     */
    protected void writeByte(final byte[] text) {
        try {
            if (text != null) {
                bao.write(text);
            }
        } catch (final Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
        bao.write(0x00);
    }

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param value
     *            - 要写入的字节
     */
    protected void writeC(final int value) {
        bao.write(value & 0xff);
    }

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param value
     *            - 要写入的字节
     */
    protected void writeD(final int value) {
        bao.write(value & 0xff);
        bao.write(value >> 8 & 0xff);
        bao.write(value >> 16 & 0xff);
        bao.write(value >> 24 & 0xff);
    }

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param value
     *            - 要写入的字节
     */
    protected void writeExp(final long value) {
        bao.write((int) (value & 0xff));
        bao.write((int) (value >> 8 & 0xff));
        bao.write((int) (value >> 16 & 0xff));
        bao.write((int) (value >> 24 & 0xff));
    }

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param value
     *            - 要写入的字节
     */
    protected void writeF(final double org) {
        final long value = Double.doubleToRawLongBits(org);
        bao.write((int) (value & 0xff));
        bao.write((int) (value >> 8 & 0xff));
        bao.write((int) (value >> 16 & 0xff));
        bao.write((int) (value >> 24 & 0xff));
        bao.write((int) (value >> 32 & 0xff));
        bao.write((int) (value >> 40 & 0xff));
        bao.write((int) (value >> 48 & 0xff));
        bao.write((int) (value >> 56 & 0xff));
    }

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param value
     *            - 要写入的字节
     */
    protected void writeH(final int value) {
        bao.write(value & 0xff);
        bao.write(value >> 8 & 0xff);
    }

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param value
     *            - 要写入的字节
     */
    protected void writeL(final long value) {
        bao.write((int) (value & 0xff));
    }

    /**
     * 将指定'字节'写入该字节数组输出流.
     * 
     * @param value
     *            - 要写入的字节
     */
    protected void writeP(final int value) {
        bao.write(value);
    }

    /**
     * 将指定'字符串'写入该字节数组输出流.
     * 
     * @param text
     *            - 要写入的字符串
     */
    protected void writeS(final String text) {
        try {
            if (text != null) {
                bao.write(text.getBytes(CLIENT_LANGUAGE_CODE));
            }
        } catch (final Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
        bao.write(0x00);
    }

    /**
     * 取得输出字节流(大小是此输出流的当前大小和缓冲区的有效内容).
     * 
     * @return 此输出流的当前内容,作为一个字节数组.
     */
    protected byte[] getBytes() {
        final int padding = bao.size() % 8; // 补位(不满8补足8)
        if (padding != 0) {
            for (int i = padding; i < 8; i++) {
                this.writeC(0x00);
            }
        }
        return bao.toByteArray();
    }

    /**
     * 取得长度.
     * 
     * @return 真实长度(实际发送出去时的长度)
     */
    protected int getLength() {
        return bao.size() + 2;
    }

    /**
     * 取得'服务端封包类型'的字符串信息.
     * 
     * @param 字符串信息
     */
    protected String getType() {
        return "[S] " + getClass().getSimpleName();
    }
}
